{% extends "base.html" %}

{% block title %}{{ page_title or "搜索" }} - 个人博客{% endblock %}

{% block content %}
<div class="search-page">
    <div class="search-content">
        {% if query %}
            <!-- 搜索结果统计 -->
            <div class="search-stats mb-6">
                <p class="text-gray-600">
                    {% if total_results > 0 %}
                        找到 <span class="font-semibold text-blue-600">{{ total_results }}</span> 篇相关文章
                    {% else %}
                        没有找到相关文章
                    {% endif %}
                </p>
                {% if category %}
                <p class="text-sm text-gray-500 mt-2">
                    <i class="fas fa-folder mr-1"></i>分类: {{ category.name }}
                </p>
                {% endif %}
                {% if tag %}
                <p class="text-sm text-gray-500 mt-2">
                    <i class="fas fa-tag mr-1"></i>标签: {{ tag.name }}
                </p>
                {% endif %}
            </div>
            
            <!-- 搜索结果列表 -->
            {% if posts and posts.items %}
                <div class="search-results">
                    <div class="results-list grid grid-cols-1 lg:grid-cols-2 gap-6">
                        {% for post in posts.items %}
                        <article class="search-result-item bg-white border border-gray-200 rounded-lg p-6 hover:shadow-md transition-shadow flex flex-col h-full">
                            <h3 class="result-title mb-3">
                                <a href="{{ post.html_path or '/post/' + post.id|string }}" class="text-xl font-semibold text-gray-900 hover:text-blue-600 transition-colors">
                                    {{ post.title }}
                                </a>
                            </h3>
                            
                            <div class="result-meta flex items-center gap-4 text-sm text-gray-500 mb-3">
                                <span>
                                    <i class="fas fa-calendar mr-1"></i>
                                    {{ post.published_at.strftime('%Y-%m-%d') if post.published_at else post.created_at.strftime('%Y-%m-%d') }}
                                </span>
                                <span>
                                    <i class="fas fa-eye mr-1"></i>
                                    {{ post.view_count or 0 }} 次浏览
                                </span>
                                {% if post.category %}
                                <span>
                                    <i class="fas fa-folder mr-1"></i>
                                    {{ post.category.name }}
                                </span>
                                {% endif %}
                            </div>
                            
                            <div class="result-excerpt text-gray-700 mb-4 flex-grow">
                                {{ post.excerpt or (post.content[:200] + '...' if post.content|length > 200 else post.content) }}
                            </div>
                            
                            {% if post.tags %}
                            <div class="result-tags mb-4">
                                {% for tag in post.tags %}
                                <span class="inline-block bg-gray-100 text-gray-700 px-3 py-1 rounded-full text-sm mr-2 mb-2">{{ tag.name }}</span>
                                {% endfor %}
                            </div>
                            {% endif %}
                            
                            <div class="result-actions mt-auto">
                                <a href="{{ post.html_path or '/post/' + post.id|string }}" class="inline-flex items-center px-4 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700 transition-colors">
                                    阅读全文
                                    <i class="fas fa-arrow-right ml-2"></i>
                                </a>
                            </div>
                        </article>
                        {% endfor %}
                    </div>
                    
                    <!-- 分页 -->
                    {% if posts.pages > 1 %}
                    <div class="mt-12">
                        <div class="bg-white rounded-lg shadow-md p-6">
                            <div class="flex items-center justify-between">
                                <!-- 分页信息 -->
                                <div class="text-sm text-gray-600">
                                    显示第 {{ (posts.page - 1) * posts.per_page + 1 }} - {{ posts.page * posts.per_page if posts.page * posts.per_page <= posts.total else posts.total }} 条，共 {{ posts.total }} 条记录
                                </div>
                                
                                <!-- 分页导航 -->
                                <nav class="flex items-center space-x-1">
                                    <!-- 首页 -->
                                    {% if posts.has_prev %}
                                        {% if category %}
                                        <a href="{{ url_for('main.category_posts', category_id=category.id, page=1) }}" class="px-3 py-2 text-gray-500 hover:text-blue-600 hover:bg-blue-50 rounded-lg transition-colors" title="首页">
                                            <i class="fas fa-angle-double-left"></i>
                                        </a>
                                        {% elif tag %}
                                        <a href="{{ url_for('main.tag_posts', tag_id=tag.id, page=1) }}" class="px-3 py-2 text-gray-500 hover:text-blue-600 hover:bg-blue-50 rounded-lg transition-colors" title="首页">
                                            <i class="fas fa-angle-double-left"></i>
                                        </a>
                                        {% else %}
                                        <a href="{{ url_for('main.search', q=query, page=1) }}" class="px-3 py-2 text-gray-500 hover:text-blue-600 hover:bg-blue-50 rounded-lg transition-colors" title="首页">
                                            <i class="fas fa-angle-double-left"></i>
                                        </a>
                                        {% endif %}
                                    {% else %}
                                    <span class="px-3 py-2 text-gray-300 rounded-lg" title="首页">
                                        <i class="fas fa-angle-double-left"></i>
                                    </span>
                                    {% endif %}
                                    
                                    <!-- 上一页 -->
                                    {% if posts.has_prev %}
                                        {% if category %}
                                        <a href="{{ url_for('main.category_posts', category_id=category.id, page=posts.prev_num) }}" class="px-3 py-2 text-gray-500 hover:text-blue-600 hover:bg-blue-50 rounded-lg transition-colors" title="上一页">
                                            <i class="fas fa-chevron-left"></i>
                                        </a>
                                        {% elif tag %}
                                        <a href="{{ url_for('main.tag_posts', tag_id=tag.id, page=posts.prev_num) }}" class="px-3 py-2 text-gray-500 hover:text-blue-600 hover:bg-blue-50 rounded-lg transition-colors" title="上一页">
                                            <i class="fas fa-chevron-left"></i>
                                        </a>
                                        {% else %}
                                        <a href="{{ url_for('main.search', q=query, page=posts.prev_num) }}" class="px-3 py-2 text-gray-500 hover:text-blue-600 hover:bg-blue-50 rounded-lg transition-colors" title="上一页">
                                            <i class="fas fa-chevron-left"></i>
                                        </a>
                                        {% endif %}
                                    {% else %}
                                    <span class="px-3 py-2 text-gray-300 rounded-lg" title="上一页">
                                        <i class="fas fa-chevron-left"></i>
                                    </span>
                                    {% endif %}
                                    
                                    <!-- 页码 -->
                                    {% for page_num in posts.iter_pages() %}
                                        {% if page_num %}
                                            {% if page_num != posts.page %}
                                                {% if category %}
                                                <a href="{{ url_for('main.category_posts', category_id=category.id, page=page_num) }}" class="px-3 py-2 text-gray-700 hover:text-blue-600 hover:bg-blue-50 rounded-lg transition-colors">{{ page_num }}</a>
                                                {% elif tag %}
                                                <a href="{{ url_for('main.tag_posts', tag_id=tag.id, page=page_num) }}" class="px-3 py-2 text-gray-700 hover:text-blue-600 hover:bg-blue-50 rounded-lg transition-colors">{{ page_num }}</a>
                                                {% else %}
                                                <a href="{{ url_for('main.search', q=query, page=page_num) }}" class="px-3 py-2 text-gray-700 hover:text-blue-600 hover:bg-blue-50 rounded-lg transition-colors">{{ page_num }}</a>
                                                {% endif %}
                                            {% else %}
                                            <span class="px-3 py-2 bg-blue-600 text-white rounded-lg font-medium">{{ page_num }}</span>
                                            {% endif %}
                                        {% else %}
                                        <span class="px-3 py-2 text-gray-400">...</span>
                                        {% endif %}
                                    {% endfor %}
                                    
                                    <!-- 下一页 -->
                                    {% if posts.has_next %}
                                        {% if category %}
                                        <a href="{{ url_for('main.category_posts', category_id=category.id, page=posts.next_num) }}" class="px-3 py-2 text-gray-500 hover:text-blue-600 hover:bg-blue-50 rounded-lg transition-colors" title="下一页">
                                            <i class="fas fa-chevron-right"></i>
                                        </a>
                                        {% elif tag %}
                                        <a href="{{ url_for('main.tag_posts', tag_id=tag.id, page=posts.next_num) }}" class="px-3 py-2 text-gray-500 hover:text-blue-600 hover:bg-blue-50 rounded-lg transition-colors" title="下一页">
                                            <i class="fas fa-chevron-right"></i>
                                        </a>
                                        {% else %}
                                        <a href="{{ url_for('main.search', q=query, page=posts.next_num) }}" class="px-3 py-2 text-gray-500 hover:text-blue-600 hover:bg-blue-50 rounded-lg transition-colors" title="下一页">
                                            <i class="fas fa-chevron-right"></i>
                                        </a>
                                        {% endif %}
                                    {% else %}
                                    <span class="px-3 py-2 text-gray-300 rounded-lg" title="下一页">
                                        <i class="fas fa-chevron-right"></i>
                                    </span>
                                    {% endif %}
                                    
                                    <!-- 末页 -->
                                    {% if posts.has_next %}
                                        {% if category %}
                                        <a href="{{ url_for('main.category_posts', category_id=category.id, page=posts.pages) }}" class="px-3 py-2 text-gray-500 hover:text-blue-600 hover:bg-blue-50 rounded-lg transition-colors" title="末页">
                                            <i class="fas fa-angle-double-right"></i>
                                        </a>
                                        {% elif tag %}
                                        <a href="{{ url_for('main.tag_posts', tag_id=tag.id, page=posts.pages) }}" class="px-3 py-2 text-gray-500 hover:text-blue-600 hover:bg-blue-50 rounded-lg transition-colors" title="末页">
                                            <i class="fas fa-angle-double-right"></i>
                                        </a>
                                        {% else %}
                                        <a href="{{ url_for('main.search', q=query, page=posts.pages) }}" class="px-3 py-2 text-gray-500 hover:text-blue-600 hover:bg-blue-50 rounded-lg transition-colors" title="末页">
                                            <i class="fas fa-angle-double-right"></i>
                                        </a>
                                        {% endif %}
                                    {% else %}
                                    <span class="px-3 py-2 text-gray-300 rounded-lg" title="末页">
                                        <i class="fas fa-angle-double-right"></i>
                                    </span>
                                    {% endif %}
                                </nav>
                            </div>
                        </div>
                    </div>
                    {% endif %}
                </div>
            {% else %}
                <!-- 无搜索结果 -->
                <div class="no-results text-center py-12">
                    <div class="text-gray-400 mb-4">
                        <i class="fas fa-search text-6xl"></i>
                    </div>
                    <h3 class="text-xl font-semibold text-gray-600 mb-2">暂无搜索结果</h3>
                    <p class="text-gray-500 mb-6">请尝试使用其他关键词搜索</p>
                    <a href="{{ url_for('main.index') }}" class="inline-flex items-center px-4 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700 transition-colors">
                        <i class="fas fa-home mr-2"></i>
                        返回首页
                    </a>
                </div>
            {% endif %}
        {% else %}
            <!-- 无搜索关键词时重定向到首页 -->
            <script>
                window.location.href = "{{ url_for('main.index') }}";
            </script>
        {% endif %}
    </div>
</div>
{% endblock %}

{% block extra_css %}
<style>
.search-page {
    padding: 10px 5px;
}

.search-header {
    text-align: center;
    margin-bottom: 40px;
    padding: 40px 0;
    background: #fff;
    border-radius: 12px;
    box-shadow: 0 4px 12px rgba(0,0,0,0.1);
}

.search-header h1 {
    font-size: 32px;
    font-weight: 700;
    color: #2c3e50;
    margin-bottom: 15px;
}

.search-header p {
    font-size: 16px;
    color: #7f8c8d;
}

.search-content {
    background: #fff;
    border-radius: 12px;
    box-shadow: 0 4px 12px rgba(0,0,0,0.1);
    padding: 25px;
}

.search-box {
    margin-bottom: 40px;
}

.search-input-group {
    display: flex;
    max-width: 600px;
    margin: 0 auto;
    border: 2px solid #e1e8ed;
    border-radius: 25px;
    overflow: hidden;
    transition: all 0.3s ease;
}

.search-input-group:focus-within {
    border-color: #3498db;
    box-shadow: 0 0 0 3px rgba(52, 152, 219, 0.1);
}

.search-input {
    flex: 1;
    padding: 15px 20px;
    border: none;
    outline: none;
    font-size: 16px;
    background: transparent;
}

.search-input::placeholder {
    color: #95a5a6;
}

.search-btn {
    padding: 15px 20px;
    background: #3498db;
    border: none;
    color: #fff;
    cursor: pointer;
    transition: background 0.3s ease;
    display: flex;
    align-items: center;
    justify-content: center;
}

.search-btn:hover {
    background: #2980b9;
}

.search-results {
    margin-top: 30px;
}

.results-header {
    margin-bottom: 30px;
    padding-bottom: 15px;
    border-bottom: 2px solid #e1e8ed;
}

.results-header h2 {
    font-size: 20px;
    font-weight: 600;
    color: #2c3e50;
}

.results-list {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 20px;
}

.search-result-item {
    padding: 18px;
    background: #f8f9fa;
    border-radius: 12px;
    border: 1px solid #e1e8ed;
    transition: all 0.3s ease;
    height: 100%;
    display: flex;
    flex-direction: column;
}

.search-result-item:hover {
    background: #fff;
    box-shadow: 0 4px 12px rgba(0,0,0,0.1);
    transform: translateY(-2px);
}

.result-title {
    margin-bottom: 20px;
}

.result-title a {
    font-size: 16px;
    font-weight: 600;
    color: #2c3e50;
    text-decoration: none;
    transition: color 0.3s ease;
    line-height: 1.3;
}

.result-title a:hover {
    color: #3498db;
}

.result-meta {
    display: flex;
    flex-wrap: wrap;
    gap: 12px;
    margin-bottom: 15px;
    color: #7f8c8d;
    font-size: 12px;
}

.result-meta span {
    display: flex;
    align-items: center;
    gap: 5px;
}

.meta-icon {
    width: 14px;
    height: 14px;
    opacity: 0.7;
}

.result-excerpt {
    color: #5a6c7d;
    line-height: 1.5;
    margin-bottom: 15px;
    font-size: 14px;
    display: -webkit-box;
    -webkit-line-clamp: 3;
    -webkit-box-orient: vertical;
    overflow: hidden;
    flex: 1;
}

.result-tags {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    margin-bottom: 20px;
}

.result-actions {
    display: flex;
    justify-content: flex-end;
    margin-top: auto;
}

.search-pagination {
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 15px;
    margin-top: 40px;
    padding-top: 30px;
    border-top: 1px solid #e1e8ed;
}

.pagination-btn {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 10px 20px;
    background: #3498db;
    color: #fff;
    text-decoration: none;
    border-radius: 6px;
    font-weight: 500;
    transition: all 0.3s ease;
}

.pagination-btn:hover {
    background: #2980b9;
    transform: translateY(-1px);
}

.pagination-pages {
    display: flex;
    gap: 8px;
}

.pagination-page {
    padding: 8px 12px;
    border: 1px solid #e1e8ed;
    color: #7f8c8d;
    text-decoration: none;
    border-radius: 4px;
    transition: all 0.3s ease;
}

.pagination-page:hover {
    background: #3498db;
    color: #fff;
    border-color: #3498db;
}

.pagination-page.current {
    background: #3498db;
    color: #fff;
    border-color: #3498db;
}

.pagination-ellipsis {
    padding: 8px 12px;
    color: #95a5a6;
}

.no-results {
    text-align: center;
    padding: 60px 20px;
}

.no-results-icon {
    color: #bdc3c7;
    margin-bottom: 20px;
}

.no-results h3 {
    font-size: 24px;
    color: #2c3e50;
    margin-bottom: 15px;
}

.no-results p {
    color: #7f8c8d;
    margin-bottom: 30px;
}

.search-suggestions {
    text-align: left;
    max-width: 400px;
    margin: 0 auto;
}

.search-suggestions h4 {
    color: #2c3e50;
    margin-bottom: 15px;
}

.search-suggestions ul {
    color: #7f8c8d;
    line-height: 1.8;
}

.search-tips {
    margin-top: 30px;
}

.search-tips h2 {
    font-size: 24px;
    font-weight: 600;
    color: #2c3e50;
    margin-bottom: 30px;
    text-align: center;
}

.tips-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
    gap: 30px;
}

.tip-item {
    text-align: center;
    padding: 30px 20px;
    background: #f8f9fa;
    border-radius: 8px;
    transition: all 0.3s ease;
}

.tip-item:hover {
    background: #e9ecef;
    transform: translateY(-2px);
}

.tip-icon {
    width: 50px;
    height: 50px;
    background: #3498db;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    margin: 0 auto 20px;
    color: #fff;
}

.tip-icon svg {
    width: 24px;
    height: 24px;
}

.tip-item h3 {
    font-size: 18px;
    font-weight: 600;
    color: #2c3e50;
    margin-bottom: 10px;
}

.tip-item p {
    color: #7f8c8d;
    line-height: 1.6;
}

</style>
{% endblock %}
